﻿/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using OptixDotNet;
using OptixDotNet.Math;
using OptixDotNet.Utils;

using OptixDotNetUtils;

namespace Sample
{
    public class Sample3 : OptixForm
    {     
        public Sample3()
        {
            //Ensure that we have a valid Nvidia GPU
            Utils.DetectGPUCompatibility();
        }

        protected override void Initialize()
        {
            //create context
            Context = new Context();
            RaysTracedPerFrame = 0;
        }

        protected override void Display()
        {
            base.Display();

            //get info
            uint version = Context.OptixVersion;
            uint major = version / 1000;
            uint minor = ( version % 1000 ) / 10;
            string numThreads = Context.CpuNumThreads.ToString();
            string mem = ( Context.AvailableMemory / 1024 / 1024 ).ToString();
            string textures = Context.MaxTextureCount.ToString();
            string pagingSupport = Context.GpuPagingActive.ToString();
            string pagingDisabled = Context.GpuPagingDisabled.ToString();

            int xPos = 10;
            int yPos = 700;
            int incr = 20;
            DrawText( xPos, yPos, "OptixVersion: " + major + "." + minor );yPos -= incr;
            DrawText( xPos, yPos, "NumCpuThreads: " + numThreads );        yPos -= incr;
            DrawText( xPos, yPos, "MemAvailable: " + mem + "MB" );         yPos -= incr;
            DrawText( xPos, yPos, "MaxTextures: " + textures );            yPos -= incr;
            DrawText( xPos, yPos, "PagingSupported: " + pagingSupport );   yPos -= incr;
            DrawText( xPos, yPos, "PagingDisabled: " + pagingDisabled );   yPos -= incr;

            Device[] devices = Context.Devices;
            yPos -= incr;
            DrawText( xPos, yPos, "DeviceInfo:" ); yPos -= incr;
            yPos -= incr;

            for ( int i = 0; i < devices.Length; i++ )
            {
                Int2 compute = devices[ i ].ComputeCapability;
                DrawText( xPos, yPos, "Device" + i + ":" );                                                 yPos -= incr;
                DrawText( xPos, yPos, "Name: " + devices[ i ].Name );                                       yPos -= incr;
                DrawText( xPos, yPos, "Compute: " + compute.X + "." + compute.Y );                          yPos -= incr;
                DrawText( xPos, yPos, "Streaming MP Count: " + devices[ i ].ProcessorCount );               yPos -= incr;
                DrawText( xPos, yPos, "ClockRate: " + ( devices[ i ].ClockRate / 1000 / 1000f ) + "GHz" );  yPos -= incr;
                DrawText( xPos, yPos, "TotalMem: " + ( devices[ i ].TotalMemory / 1024f / 1024f ) + "MB" ); yPos -= incr;
                DrawText( xPos, yPos, "Threads/Block: " + devices[ i ].MaxThreadsPerBlock );                yPos -= incr;
                DrawText( xPos, yPos, "MaxHWTextures: " + devices[ i ].MaxHardwareTextureCount );           yPos -= incr;
                DrawText( xPos, yPos, "TimeoutEnabled: " + devices[ i ].ExecutionTimeOutEnabled );          yPos -= incr;
                yPos -= incr;
                xPos += 250;
            }
        }
    }
}
